package jp.co.cos.ittrick.channel.controller.admin;

import jp.co.cos.ittrick.channel.model.ChannelModel;
import jp.co.cos.ittrick.channel.service.ChannelService;
import jp.co.cos.ittrick.channel.service.FileService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

public class ChannelEditController extends Controller {

    private ChannelService channelService;
    private FileService fileService;

    public ChannelEditController() {
        channelService = new ChannelService();
        fileService = new FileService();
    }

    @Override
    public Navigation run() throws Exception {

        String channel = asString("channel");
        ChannelModel channelModel = channelService.getChannelByKey(channel);

        // カスタムタグのエンエスケープ
        String content = channelModel.getContentString();
        channelModel.setContent(new Text(content));

        requestScope("channelModel", channelModel);

        // キーワード
        String keyWord = null;
        for(String keyWordTmp: channelModel.getKeyWordList()) {
            if(keyWord == null) {
                keyWord = keyWordTmp;
            }else {
                keyWord = keyWord + "," + keyWordTmp;
            }
        }
        requestScope("keyWord", keyWord);

        // ファイル
        requestScope("dataList", fileService.getDataListOfChannel(channel));

        return forward("channelEdit.jsp");
    }
}
